package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.CommentConstant;
import com.sky.constant.DefaultImageConstant;
import com.sky.dto.ArticleUpdateDTO;
import com.sky.dto.CommentPageDTO;
import com.sky.dto.UserCommentDTO;
import com.sky.entity.Article;
import com.sky.entity.User;
import com.sky.exception.CommentSendException;
import com.sky.mapper.ArticleMapper;
import com.sky.mapper.CommentMapper;
import com.sky.mapper.ReplyMapper;
import com.sky.mapper.UserMapper;
import com.sky.result.PageResult;
import com.sky.service.CommentService;
import com.sky.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class CommentServiceImpl implements CommentService {

    private final ReplyMapper replyMapper;

    private final RedisTemplate<String ,String> redisTemplate;

    private final CommentMapper commentMapper;

    private final UserMapper userMapper;

    private final ArticleMapper articleMapper;

    @Autowired
    public CommentServiceImpl(CommentMapper commentMapper,
                              UserMapper userMapper,
                              RedisTemplate<String,String> redisTemplate,
                              ArticleMapper articleMapper,
                              ReplyMapper replyMapper) {
        this.commentMapper = commentMapper;
        this.userMapper = userMapper;
        this.redisTemplate = redisTemplate;
        this.articleMapper = articleMapper;
        this.replyMapper = replyMapper;
    }

    /**
     * 分页查询
     * @param commentPageDTO
     * @return
     */
    @Override
    public PageResult list(CommentPageDTO commentPageDTO) {
        PageHelper.startPage(commentPageDTO.getPage(), commentPageDTO.getPageSize());

        Page<CommentVO> page = commentMapper.list(commentPageDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增评论
     * @param userCommentDTO
     */
    @Override
    public void add(UserCommentDTO userCommentDTO) {

        Long id = userCommentDTO.getArticleId();

        //根据id查询文章
        Article article = articleMapper.getById(id);

        //文章评论加一
        Long commentCount = article.getCommentCount() + 1;
        ArticleUpdateDTO articleUpdateDTO = ArticleUpdateDTO.builder()
                .id(id)  // 设置文章ID
                .commentCount(commentCount)
                .build();
        articleMapper.updateArticle(articleUpdateDTO);


        //根据昵称查询user是否存在
        User user = userMapper.selectByUsername(userCommentDTO.getNickname());


        //如果不存在则新增user
        if(user == null){

            user = User.builder()
                    .nickname(userCommentDTO.getNickname())
                    .email(userCommentDTO.getEmail())
                    .avatar(DefaultImageConstant.MAN)
                    .build();
            userMapper.insertUser(user);
        }

        //验证邮箱验证码
        //获取验证码
        String code = redisTemplate.opsForValue().get(userCommentDTO.getEmail());
        if(code == null){
            //验证码过期
            throw new CommentSendException(CommentConstant.CODE_EXPIRE);
        }
        if(!code.equals(userCommentDTO.getCode())){
            //验证码错误
            throw new CommentSendException(CommentConstant.CODE_ERROR);
        }

        //新增comment
        commentMapper.addComment(userCommentDTO);
    }

    /**
     * 根据评论id删除评论
     * @param id
     */
    @Override
    public void deleteComment(Long id) {

        //根据评论id查询comment
        CommentVO commentVO = commentMapper.getById(id);

        //根据文章id查询文章
        Article article = articleMapper.getById(commentVO.getArticleId());

        //文章评论减一
        if(article.getCommentCount() > 0){
            Long commentCount = article.getCommentCount() - 1;
            ArticleUpdateDTO articleUpdateDTO = ArticleUpdateDTO.builder()
                    .id(commentVO.getArticleId())  // 设置文章ID
                    .commentCount(commentCount)
                    .build();
            articleMapper.updateArticle(articleUpdateDTO);
        }else{
            //文章评论为0
            throw new CommentSendException(CommentConstant.ARTICLE_COMMENT_ZERO);
        }

        //删除回复
        //如果评论id存在则删除回复
        if(replyMapper.getReplyCommentId(id) != null){
            replyMapper.deleteReplyByCommentId(id);
        }


        commentMapper.deleteComment(id);
    }

    /**
     * 统计评论数量
     * @return
     */
    @Override
    public Long countComment() {
        return commentMapper.countComment();
    }

    /**
     * 根据文章id统计评论数量
     */
    @Override
    public Long countByArticleId(Long articleId) {
        return commentMapper.countByArticleId(articleId);
    }


}
